﻿Imports System.Data.SqlClient
Public Class clsCot_Documentos
    Private mintIdDocumento As Integer
    Private mintFolio As Integer
    Private mdatFecha As Date
    Private mintNoCotizaciones As Integer
    Private mintProcesada As Integer
    Private mstrConcepto As String
    Public Property IdDocumento() As Integer
        Get
            Return mintIdDocumento
        End Get
        Set(ByVal value As Integer)
            mintIdDocumento = value
        End Set
    End Property
    Public Property Folio() As Integer
        Get
            Return mintFolio
        End Get
        Set(ByVal value As Integer)
            mintFolio = value
        End Set
    End Property
    Public Property Fecha() As Date
        Get
            Return mdatFecha
        End Get
        Set(ByVal value As Date)
            mdatFecha = value
        End Set
    End Property
    Public Property NoCotizaciones() As Integer
        Get
            Return mintNoCotizaciones
        End Get
        Set(ByVal value As Integer)
            mintNoCotizaciones = value
        End Set
    End Property
    Public Property Procesada() As Integer
        Get
            Return mintProcesada
        End Get
        Set(ByVal value As Integer)
            mintProcesada = value
        End Set
    End Property
    Public Property Concepto() As String
        Get
            Return mstrConcepto
        End Get
        Set(ByVal value As String)
            mstrConcepto = value
        End Set
    End Property

    Public Sub New()
        mintIdDocumento = 0
        mintFolio = 0
        mdatFecha = Now()
        mintNoCotizaciones = 0
        mintProcesada = 0
        mstrConcepto = ""
    End Sub
    Public Sub Buscar()
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = "SELECT * From Cot_Documentos " & _
        " WHERE Folio=" & Folio

        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        If rdBuscar.Read() Then
            mdatFecha = rdBuscar("Fecha")
            mintNoCotizaciones = rdBuscar("NoCotizaciones")
            mintProcesada = rdBuscar("Procesada")
            mstrConcepto = rdBuscar("Concepto").ToString.Trim
        Else
            mdatFecha = Now()
            mintNoCotizaciones = 0
            mintProcesada = 0
            mstrConcepto = ""
        End If

        rdBuscar.Close()
        cnConn.Close()


    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into Cot_Documentos (" & _
                            "Folio," & _
                            "Fecha," & _
                            "NoCotizaciones," & _
                            "Procesada," & _
                            "Concepto)" & _
                            "VALUES (" & _
                            "@Folio," & _
                            "@Fecha," & _
                            "@NoCotizaciones," & _
                            "@Procesada," & _
                            "substring(@Concepto,1,200))" & _
                            " SELECT SCOPE_IDENTITY()"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("Folio", mintFolio)
        Command.Parameters.AddWithValue("Fecha", mdatFecha)
        Command.Parameters.AddWithValue("NoCotizaciones", mintNoCotizaciones)
        Command.Parameters.AddWithValue("Procesada", mintProcesada)
        Command.Parameters.AddWithValue("Concepto", mstrConcepto)

        cnConn.Open()
        IdDocumento = Convert.ToInt32(Command.ExecuteScalar)

        If IdDocumento > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el documento ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
End Class
